package concurrent.producerconsumer;

import utils.DateTimeUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Author: Jason Wu
 * Date  : 2014-03-12
 */
public class TestBatchExecute {
    public static void main(String[] args) throws InterruptedException {
        Collection<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
        for (int i = 0; i < 100; i++) {
            final int index = i;
            tasks.add(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(1000);
                    String time = DateTimeUtils.formatDateTime(new Date());
                    System.out.println(String.format("taskId:%s, time:%s", index, time));
                    return index;
                }
            });
        }
        DynamicThreadPool executor = new DynamicThreadPool(new LinkedBlockingQueue(), 3);
        new Thread(new UpdateThreadCount(executor)).start();
        ExecutorUtils.batchExecute(executor, tasks);
        System.out.println(tasks);
    }
}
